<template>
  <div class="home">
    <header>
      商品列表
    </header>
    <section class="center">
      <span v-for="(item,index) in list" :key="index" :class="{active:checkIndex==index}" @click="cut(index)">{{item.name}}</span>
    </section>
    <ul>
      <li v-for="(item,index) in list[checkIndex]?.child" :key="index">
        <img src="../assets/img.png" alt="">
        <div>
           <h4>{{item.name}}</h4>
           <p>${{item.price}}</p>
           <section>
             <span>{{item.payNum}}人付款</span>
             <button @click="jump(item)">+</button>
           </section>
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
import { mapActions,mapState,mapMutations } from 'vuex';
export default {
  name: 'Home',
  components: {

  },
  data(){
    return{
      checkIndex: 0
    }
  },
  computed:{
    ...mapState(['list'])
  },
  created() {
    this.getList();
  },
  methods: {
    ...mapActions(['getList']),
    ...mapMutations(['addCart']),
    cut(i){
      this.checkIndex = i;
    },
    jump(val){ // 加入购物车的函数
      this.addCart(val)
      this.$router.push('/about')
    }
  },
}
</script>
<style lang="scss" scoped>
  header{
    height: 45px;
    line-height: 45px;
    text-align: center;
  }
  .center{
    height: 50px;
    background: #440f7d;
    color: #fff;
    display: flex;
    align-items: center;
    span{
      width: 20%;
      text-align: center;
      line-height: 50px;
    }
    .active{
      background: #8d3ec5;
    }
  }
  li{
    display: flex;
    margin-bottom: 10px;
    
    div{
      flex: 1;
      padding: 10px;
       h4{
          margin-top: 10px;
        }
        p{
          color: #8d3ec5;
          margin: 5px 0;
        }
      section{
        button{
          float: right;
          border: none;
          background:#8d3ec5;
          color: #fff;
          padding: 2px 10px;
          border-radius: 10px;
        }
      }
    }
  }
</style>
